#!/bin/sh

# Makefile passes in arg 1
PYTHON=$1

echo -n "Checking ORACLE_HOME..."

if [ -z "$ORACLE_HOME" ]; then
	echo "failed."
	echo "Your ORACLE_HOME environment variable is not set up; please"
	echo "properly configure your Oracle environment and re-run this"
	echo "program."
	exit 1
fi

echo "passed."

echo -n "Checking for Oracle version..."

# Thanks to Wilfredo Sanchez for the MacOS X patch
if [ -r "$ORACLE_HOME/lib/libclntsh.so" ]; then
	LIBCLNTSH=libclntsh.so
elif [ -r "$ORACLE_HOME/lib/libclntsh.dylib" ]; then
	LIBCLNTSH=libclntsh.dylib
else
	echo "failed."
	echo "$ORACLE_HOME/lib/libclntsh.so not readable; unable to"
	echo "determine your oracle version."
	exit 2
fi

#
# Check for the symbol OCIEnvCreate in libclntsh.so, indicating Oracle 8i
#
nm $ORACLE_HOME/lib/$LIBCLNTSH | grep OCIConnectionPoolCreate > /dev/null

if [ "$?" = "0" ]; then
	ORADEF="-DORACLE9i" 
	ORAINCLUDES=""
	echo "Oracle 9i found."
else
	nm $ORACLE_HOME/lib/$LIBCLNTSH | grep OCIEnvCreate > /dev/null
 	if [ "$?" = "0" ]; then
		ORADEF="-DORACLE8i" 
		ORAINCLUDES=""
		echo "Oracle 8i found."
	else
		ORADEF=""
		ORAINCLUDES="-lcommon -lcore4 -lnlsrtl3"
		echo "Oracle 8.0 assumed."
	fi
fi

if [ ! -f "$ORACLE_HOME/rdbms/demo/xa.h" ]; then
	ORADEF="$ORADEF -DUSEOWNXID"
fi

#
# OS specific section
#

os=`uname -s`

case $os in
	Linux)
		LDRELOCCOMMAND="-Wl,-rpath,\$(ORACLE_HOME)\/lib"
		PBIN=`which $PYTHON`
		ldd $PBIN | grep -q libpthread 2>&1 > /dev/null
		if [ $? != 0 ]; then
			echo ""
			echo "+----------> Library Incompatibility <-----------"
			echo "| Your Python binary is not linked against"
			echo "| libpthread.  Oracle support requires libpthread."
			echo "+------------------------------------------------"
			echo ""
			exit 3
		fi

	;;
	SunOS)
		LDRELOCCOMMAND="-R \$(ORACLE_HOME)\/lib"
	;;
	Irix)
		ORAINCLUDES="-lclntshcdk"	# Chad Smith suggested this
		ORADEF="-DORACLE8i"	# Comment out if you're using 8.0
	;;
	*)
		LDRELOCCOMMAND=""
	;;
esac

#
# Now add any additional defines to the Setup.in file
#

sed -e "s/LDRELOCCOMMAND/$LDRELOCCOMMAND/" -e "s/-DCONFIGDEFINES/$ORADEF/" -e "s/-LORAINCLUDES/$ORAINCLUDES/"< Setup.in.dco > Setup.in
